package zj.it.bhne.gridengineeringsurvey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import zj.it.bhne.gridengineeringsurvey.BaseActivity;
import zj.it.bhne.gridengineeringsurvey.MyApplication;
import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Items;
import zj.it.bhne.gridengineeringsurvey.myutils.MyUtils;

import static zj.it.bhne.gridengineeringsurvey.activity.EquActivity.setSpinnerItemSelectedByValue;

/**
 * 返回二维码扫描结果的活动
 */
public class ResultActivity extends BaseActivity implements View.OnClickListener {


    private EditText result_ed_name;
    private Spinner result_sp_dengji;
    private Spinner result_sp_leixing;
    private EditText result_ed_note;
    private EditText result_ed_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_result);

        initView();

        Bundle extras = getIntent().getExtras();

        if (null != extras) {
            String result = extras.getString("result");

            //Log.e("TAG", "result: "+ result);
            try {
                JSONObject object = new JSONObject(result);
                setSpinnerItemSelectedByValue(result_sp_dengji, object.getString("dengji"));
                setSpinnerItemSelectedByValue(result_sp_leixing, object.getString("leixing"));
                result_ed_name.setText(object.getString("name"));
                result_ed_note.setText(object.getString("note"));
                result_ed_time.setText(object.getString("time"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //mResultText.setText(result);

           /* Bitmap barcode = null;
            byte[] compressedBitmap = extras.getByteArray(DecodeThread.BARCODE_BITMAP);
            if (compressedBitmap != null) {
                barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
                // Mutable copy:
                barcode = barcode.copy(Bitmap.Config.RGB_565, true);
            }

            mResultImage.setImageBitmap(barcode);*/
        }
    }

    private void initView() {
        ImageButton activity_ib_back = findViewById(R.id.activity_ib_back);
        TextView activity_tv_title = findViewById(R.id.activity_tv_title);
        TextView activity_tv_save = findViewById(R.id.activity_tv_save);
        activity_tv_title.setText(getString(R.string.one_eight));
        activity_tv_save.setText("");
        activity_ib_back.setOnClickListener(this);
        result_ed_name = findViewById(R.id.result_ed_name);


        result_sp_dengji = findViewById(R.id.result_sp_dengji);
        result_sp_leixing = findViewById(R.id.result_sp_leixing);
        result_ed_note = findViewById(R.id.result_ed_note);
        Button result_btn_save = findViewById(R.id.result_btn_save);
        result_btn_save.setOnClickListener(this);


        result_ed_time = findViewById(R.id.result_ed_time);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_ib_back:
                startActivity(new Intent(ResultActivity.this, BottomActivity.class));
                break;
            case R.id.result_btn_save:
                save();
                break;
        }
    }

    /**
     * 保存二维码创建的项目
     */
    private void save() {
        if (result_ed_name.getText().toString().equals("") && result_ed_time.getText().toString().equals("")) {
            MyUtils.getToast(getApplicationContext(), "项目导入错误");
        } else {
            Items items = new Items();
            items.setName(result_ed_name.getText().toString().trim());
            items.setId(null);
            items.setNote(result_ed_note.getText().toString().trim());
            items.setIsCheck(true);
            items.setTime(result_ed_time.getText().toString().trim());
            items.setState("本地");
            items.setVoltage(result_sp_dengji.getSelectedItem().toString().trim());
            items.setType(result_sp_leixing.getSelectedItem().toString().trim());
            long num = MyApplication.getInstances().getItemsDao().insert(items);
            if (num > 0) {
                MyUtils.getToast(getApplicationContext(), "创建成功");
                Intent intent = new Intent(getApplicationContext(), BottomActivity.class);
                intent.putExtra("item_id", 1);
                startActivity(intent);
            } else {
                MyUtils.getToast(getApplicationContext(), "创建失败");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }


}
